# src/models/sys_objects_field.py
from sqlalchemy import Column, String, DateTime, Boolean, Integer, Text, Index
from src.core.ormdb import Base
from src.core import str_utils as strU


class SysObjectsField(Base):
    __tablename__ = "sys_objects_field"

    # 主键
    id = Column(String(36), primary_key=True, default=lambda: strU.generate_id())

    # 业务关联
    object_id = Column(String(36), nullable=False, comment="对象ID")
    api_id = Column(String(36), nullable=False, comment="API ID")

    # 字段元数据
    field = Column(String(64), nullable=False, comment="字段名称")
    label = Column(String(64), nullable=False, comment="字段标签")
    description = Column(String(255), nullable=True, comment="字段描述")

    width = Column(Integer, default=100, comment="表格宽度")
    col_span = Column(Integer, default=24, comment="栅格数（总数24）")
    rows_num = Column(Integer, default=1, comment="每行列数")
    hide = Column(Boolean, default=False, comment="隐藏")
    read_only = Column(Boolean, default=False, comment="只读")
    input_type = Column(String(20), default="text", comment="输入类型")
    dict_id = Column(String(36), comment="字典ID")
    is_null = Column(Boolean, default=True)
    is_filter = Column(Boolean, default=False, comment="是否可筛选")
    sortable = Column(Boolean, default=False, comment="表格排序")

    # validation = Column(Text)
    align = Column(String(20), default="left", comment="对齐方式")
    color = Column(String(20), nullable=True, comment="颜色")

    # 排序 & 开关
    enabled = Column(Boolean, default=True, comment="是否启用")
    sort = Column(Integer, default=0, comment="排序")

    # 软删除
    deletion_mark = Column(Boolean, default=False, comment="删除标志，0表示未删除，1表示已删除")
    deletion_reason = Column(String(255), nullable=True, comment="删除原因，记录删除的具体原因")
    deletion_date = Column(DateTime, nullable=True, comment="删除日期，记录删除的时间")
    deletion_by = Column(String(255), nullable=True, comment="删除者，记录删除操作的执行者")

    # 审计
    create_date = Column(DateTime, default=lambda: strU.now(), comment="创建日期，记录创建的时间")
    creator_by = Column(String(255), nullable=True, comment="创建者，记录创建操作的执行者")
    last_updated_date = Column(DateTime, nullable=True, comment="最后更新时间，记录最后更新的时间")
    last_updated_by = Column(String(255), nullable=True, comment="最后更新者，记录最后更新操作的执行者")

    # 通用索引
    __table_args__ = (
        Index("idx_sysMenu_object_id", "object_id"),
        Index("idx_sysMenu_api_id", "api_id"),
        {"comment": "对象字段名称（元数据）"},
    )
